/*
Copyright 2005-2015 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.swing.dings.base;

import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.metal.MetalComboBoxIcon;

public class J42JComboBox<T> extends JComboBox<T> {

    private static final long serialVersionUID = 1L;

    private final String      buttonText;
    private transient JButton button;

    public J42JComboBox() {
        this("Color", (T[]) new Object[] { "Red", "Green", "Blue" });
    }

    public J42JComboBox(final String buttonText, final T[] value) {
        super(value);
        super.setName(buttonText);
        super.setCursor(new Cursor(Cursor.HAND_CURSOR));
        super.setOpaque(false);
        super.setBorder(BorderFactory.createEmptyBorder(2, 2, 0, 2));
        this.buttonText = buttonText;
        button.setText(buttonText);
    }

    @Override
    public void setUI(final ComboBoxUI ui) {
        super.setUI(new MyUI());
    }

    private class MyUI extends javax.swing.plaf.metal.MetalComboBoxUI {
        @Override
        protected JButton createArrowButton() {
            //final boolean iconOnly = true;
            //button = new MetalComboBoxButton(comboBox, new MetalComboBoxIcon(), iconOnly, currentValuePane, listBox);
            button = new JButton(new MetalComboBoxIcon());
            button.setMargin(new Insets(0, 1, 1, 3));
            button.setRolloverEnabled(false);
            button.setText(buttonText);
            return button;
        }
    }

    public static void main(final String[] args) {

        final JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final J42JComboBox<String> j1 = new J42JComboBox<>("label", new String[] { "value", "value2" });
        final J42JComboBox<String> j2 = new J42JComboBox<>("label", new String[] { "value", "value2" });
        f.getContentPane().setLayout(new FlowLayout());
        f.getContentPane().add(j1);
        f.getContentPane().add(j2);

        f.setSize(300, 200);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
